function building
  close all
  k=1.e08; m=25000; d0 = 0.01; omega = 55;
  tspan = [0,5];
  initial_w = [0,0,0,0];
  
  [t_vals,sol_vals] = ode45(@(t,w) eom(t,w,k,m,d0,omega),tspan,initial_w);
  plot1 = plot(t_vals,sol_vals(:,1:2),'Linewidth',1);
  set(plot1(1),'DisplayName','z_1');
  set(plot1(2),'DisplayName','z_2');
  set(gca,'FontSize',14);
  xlabel('Time (s)');
  ylabel('Displacements (m)');
  legend(gca,'show');
  grid on
  plot_building(t_vals,sol_vals,d0,omega);
end
function dwdt = eom(t,w,k,m,d0,omega)
   z1 = w(1); z2 = w(2); v1 = w(3); v2 = w(4);
   dz1dt = v1;
   dz2dt = v2;
   Fs1 = k*(z1-d0*sin(omega*t));
   Fs2 = k*(z2-z1);
   dv1dt = 2*(Fs2-Fs1)/m;
   dv2dt = -2*Fs2/m;
   dwdt = [dz1dt;dz2dt;dv1dt;dv2dt];
end
function plot_building(times,sols,d0,omega)

h = 0.5;
width = 2;
sols(:,1) = sols(:,1) + 1 + h;
sols(:,2) = sols(:,2) + 2 + 2*h;

dmax = 0.05;
size = max(sols(:,2)+2*h);
sp_w = 0.2;
figure
for i = 1:length(times)

d = d0*sin(omega*times(i));
h1 = sols(i,1);
h2 = sols(i,2);
clf
axis equal
axis square
hold all
axis([-size size -dmax-h size-dmax-h]);
plotspring([-0.35*width,d],[-0.35*width,h1-h/2],sp_w);
plotspring([0.35*width,d],[0.35*width,h1-h/2],sp_w);
plotspring([-0.35*width,h1+h/2],[-0.35*width,h2-h/2],sp_w);
plotspring([0.35*width,h1+h/2],[0.35*width,h2-h/2],sp_w);
rectangle('Position',[-width/2,d-h,width,h],'FaceColor','k');
rectangle('Position',[-width/2,h1-h/2,width,h],'FaceColor','r');
rectangle('Position',[-width/2,h2-h/2,width,h],'FaceColor','r');
pause(0.05);
end
end
